package controller;
import model.dao.*;
import model.entities.*;
import model.bs.*;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.util.*;
import java.io.PrintWriter;
/**
 * Servlet implementation class RefreshChat
 */
@WebServlet("/RefreshChat")
public class RefreshChat extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RefreshChat() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//Refresh the chat box periodically
		String message="";
		ArrayList<ChatClass> chats=new ChatView().getAllMessages();
		ChatClass c=null;
		try{
			Iterator<ChatClass> it=chats.iterator();
			while(it.hasNext()){
				c=it.next();
				message+="<b>("+c.getTimestamp()+")</b> <font color='blue'> "+c.getSender()+"</font> >>"+c.getMessage();
			}
			PrintWriter out=response.getWriter();
			out.print(message);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
